package cn.iocoder.yudao.module.trade.delivery.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.thirdparty.zxhx.ZXHXApiClientFactory;
import cn.iocoder.yudao.framework.thirdparty.zxhx.dto.ZXHXExpressItemRespDTO;
import cn.iocoder.yudao.module.trade.delivery.dal.dataobject.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.delivery.dal.mysql.DeliveryExpressMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.module.api.supply.ConfigConstants.SUPPLY_NAME_ZXHX;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/03/04
 */
@Service
@Validated
@Slf4j
public class ZXHXExpressSyncServiceImpl implements ZXHXExpressSyncService {

    @Resource
    private DeliveryExpressMapper deliveryExpressMapper;

    @Override
    public int autoSync() {
        List<ZXHXExpressItemRespDTO> expressItemRespDTOS = ZXHXApiClientFactory.commonClient().getExpress();
        if (CollUtil.isEmpty(expressItemRespDTOS)) {
            log.warn("供应链没有获取到快递公司信息");
            return 0;
        }
        int count = 0;
        for (ZXHXExpressItemRespDTO expressItemRespDTO : expressItemRespDTOS) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                log.warn("供应链同步快递公司信息线程休眠一定的毫秒数出错：{}", e.getMessage());
            }
            SpringUtil.getBean(getClass()).syncExpress(expressItemRespDTO);
            count++;
        }
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    public void syncExpress(ZXHXExpressItemRespDTO expressItemRespDTO) {
        DeliveryExpressDO expressByCode = deliveryExpressMapper.selectByCode(expressItemRespDTO.getShortName());
        if (expressByCode != null) {
            log.warn("编码【{}】对应的快递公司【{}】已存在，禁用平台的，使用供应链的", expressItemRespDTO.getShortName(), expressItemRespDTO.getName());
            expressByCode.setStatus(CommonStatusEnum.DISABLE.getStatus());
            deliveryExpressMapper.updateById(expressByCode);
        }
        DeliveryExpressDO expressDO = deliveryExpressMapper.selectBySupplyRelatedId(expressItemRespDTO.getId());
        if (expressDO == null) {
            expressDO = new DeliveryExpressDO();
            expressDO.setCode(expressItemRespDTO.getShortName());
            expressDO.setName(expressItemRespDTO.getName());
            expressDO.setSort(Math.toIntExact(expressItemRespDTO.getId()));
            expressDO.setStatus(CommonStatusEnum.ENABLE.getStatus());
            expressDO.setSupplyName(SUPPLY_NAME_ZXHX);
            expressDO.setSupplyRelatedId(expressItemRespDTO.getId());
            deliveryExpressMapper.insert(expressDO);
        } else {
            expressDO.setCode(expressItemRespDTO.getShortName());
            expressDO.setName(expressItemRespDTO.getName());
            expressDO.setStatus(CommonStatusEnum.ENABLE.getStatus());
            deliveryExpressMapper.updateById(expressDO);
        }
    }

}
